package me.a_littlesquid.origin.command;


import me.a_littlesquid.origin.FileManager;
import me.a_littlesquid.origin.items.book;
import me.a_littlesquid.origin.origins.originlist;
import me.a_littlesquid.origin.utils.*;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.Plugin;

import java.io.File;

public class origin implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        //构建类
        Plugin config=me.a_littlesquid.origin.Origin.getProvidingPlugin(me.a_littlesquid.origin.Origin.class);
        File filedata=new File(me.a_littlesquid.origin.Origin.getProvidingPlugin(me.a_littlesquid.origin.Origin.class).getDataFolder(),"data.yml");
        FileConfiguration data= YamlConfiguration.loadConfiguration(filedata);
        Prefix prefixclass=new Prefix();
        String prefix= prefixclass.getprefix();
        Random random=new Random(data,filedata);
        Player player = (Player) sender;
        //命令执行
        if(args.length==0){
            sender.sendMessage(ChatColor.AQUA + "——————" + ChatColor.GOLD + "欢迎使用起源系统" + ChatColor.AQUA + "——————");
            sender.sendMessage(prefix + ChatColor.YELLOW + "主指令" + ChatColor.WHITE + " /origin");
            sender.sendMessage(prefix + ChatColor.YELLOW + "随机生成起源" + ChatColor.WHITE + " /origin set sj ");
            Descripition.show(player,data,filedata);
        }
        if (args.length==1){
            String nextcommand=args[0];
            if (nextcommand.equalsIgnoreCase("reload")){
                if (sender.hasPermission("origin.reload")){
                    FileManager.reloadfile(data,filedata);
                    //储存文件
                    config.saveConfig();
                    config.reloadConfig();
                    sender.sendMessage(prefix+ChatColor.GREEN+"数据储存成功");
                }
                else {
                    sender.sendMessage(ChatColor.RED+"你并没有权限");
                }
            }
            if(nextcommand.equalsIgnoreCase("book")){
                PlayerInventory playerInventory=player.getInventory();
                playerInventory.addItem(book.getbook());
            }
            if(nextcommand.equalsIgnoreCase("remove")){
                remove.removepotion(player,data,filedata);
            }
        }
        if (args.length==2){
            String next1=args[0];
            String next2=args[1];
            if (next1.equalsIgnoreCase("set")&&next2.equalsIgnoreCase("sj")) {
                String value = random.Ramdonorigin();
                if (!check.hasorigin(player,data,filedata)){
                    originlist originlist = new originlist(data, filedata, player);
                    switch (value) {
                        case "ck":
                            originlist.setck();
                            sender.sendMessage(prefix + ChatColor.GREEN + "通过起源之书，你的起源是：" + ChatColor.GOLD + "刺客");
                            break;
                        case "zt":
                            originlist.setzt();
                            sender.sendMessage(prefix + ChatColor.GREEN + "通过起源之书，你的起源是：" + ChatColor.GOLD + "战坦");
                            break;
                        case "zzgw":
                            originlist.setzzgw();
                            sender.sendMessage(prefix + ChatColor.GREEN + "通过起源之书，你的起源是：" + ChatColor.GOLD + "建筑怪物");
                            break;
                        case "xt":
                            originlist.setxt();
                            sender.sendMessage(prefix + ChatColor.GREEN + "通过起源之书，你的起源是：" + ChatColor.GOLD + "血徒");
                            break;
                        case "qs":
                            originlist.setqs();
                            sender.sendMessage(prefix + ChatColor.GREEN + "通过起源之书，你的起源是：" + ChatColor.GOLD + "骑士");
                            break;
                        case "my":
                            originlist.setmy();
                            sender.sendMessage(prefix + ChatColor.GREEN + "通过起源之书，你的起源是：" + ChatColor.GOLD + "梦魇");
                            break;
                        case "zs":
                            originlist.setzs();
                            sender.sendMessage(prefix + ChatColor.GREEN + "通过起源之书，你的起源是：" + ChatColor.GOLD + "壮士");
                            break;
                        case "pt":
                            originlist.setpt();
                            sender.sendMessage(prefix + ChatColor.GREEN + "通过起源之书，你的起源是：" + ChatColor.GOLD + "普通人");
                            break;
                    }
                }
                else{
                    sender.sendMessage(ChatColor.RED+"你已经拥有了一个起源");
                }
            }
        }
        if(args.length==3) {
         if(sender.hasPermission("origin.set")){
            String next1 = args[0];
            String next2 = args[1];
            String next3 = args[2];
            Player p = Bukkit.getPlayer(next3);
            String pn=p.getDisplayName();
            if (next1.equalsIgnoreCase("set") && next2.equalsIgnoreCase("clear")) {
                remove.removepotion(p, data, filedata);
                remove.removeequipment(p);
                LivingEntity entity=p;
                entity.setMaxHealth(20);
                data.set("playerlist."+pn, null);
                FileManager.reloadfile(data, filedata);
                sender.sendMessage(prefix + ChatColor.GREEN + "删除成功");
                p.sendMessage(prefix + ChatColor.RED + "你的起源已被清除");
            }
            if (next1.equalsIgnoreCase("set")) {
                originlist originlist = new originlist(data, filedata, p);
                if (check.hasorigin(p, data, filedata)) {
                    remove.removepotion(p, data, filedata);
                    remove.removeequipment(p);
                    LivingEntity entity=p;
                    entity.setMaxHealth(20);
                    data.set("playerlist." + next3, null);
                    FileManager.reloadfile(data, filedata);
                }
                switch (next2) {
                    case "ck":
                        originlist.setck();
                        p.sendMessage(prefix + ChatColor.GREEN + "切换成功，你的起源是：" + ChatColor.GOLD + "刺客");
                        sender.sendMessage(prefix+ChatColor.GREEN+"切换成功");
                        break;
                    case "zt":
                        originlist.setzt();
                        p.sendMessage(prefix + ChatColor.GREEN + "切换成功，你的起源是：" + ChatColor.GOLD + "战坦");
                        sender.sendMessage(prefix+ChatColor.GREEN+"切换成功");
                        break;
                    case "zzgw":
                        originlist.setzzgw();
                        p.sendMessage(prefix + ChatColor.GREEN + "切换成功，你的起源是：" + ChatColor.GOLD + "建筑怪物");
                        sender.sendMessage(prefix+ChatColor.GREEN+"切换成功");
                        break;
                    case "xt":
                        originlist.setxt();
                        p.sendMessage(prefix + ChatColor.GREEN + "切换成功，你的起源是：" + ChatColor.GOLD + "血徒");
                        sender.sendMessage(prefix+ChatColor.GREEN+"切换成功");
                        break;
                    case "qs":
                        originlist.setqs();
                        p.sendMessage(prefix + ChatColor.GREEN + "切换成功，你的起源是：" + ChatColor.GOLD + "骑士");
                        sender.sendMessage(prefix+ChatColor.GREEN+"切换成功");
                        break;
                    case "my":
                        originlist.setmy();
                        p.sendMessage(prefix + ChatColor.GREEN + "切换成功，你的起源是：" + ChatColor.GOLD + "梦魇");
                        sender.sendMessage(prefix+ChatColor.GREEN+"切换成功");
                        break;
                    case "zs":
                        originlist.setzs();
                        p.sendMessage(prefix + ChatColor.GREEN + "切换成功，你的起源是：" + ChatColor.GOLD + "壮士");
                        sender.sendMessage(prefix+ChatColor.GREEN+"切换成功");
                        break;
                    case "pt":
                        originlist.setpt();
                        p.sendMessage(prefix + ChatColor.GREEN + "切换成功，你的起源是：" + ChatColor.GOLD + "普通人");
                        sender.sendMessage(prefix+ChatColor.GREEN+"切换成功");
                        break;
                }
            }
         }
        }
        return false;
    }
}